/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2023-10-03
 * Time: 16:22
 */
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return head;
        }

        //如果只有一个节点
        if(head.next == null) {
            return head;
        }

        ListNode cur = head.next;
        head.next = null;

        while(cur != null) {//把除了第一个的后面所有节点都遍历完
            ListNode curNext = cur.next;
            cur.next = head;//与头插法的思路一致
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
